//
//  SampleReelsController.m
//  CBDWallpapar
//
//  Created by AD ` J on 2019/7/20.
//  Copyright © 2019年 CBD工作室. All rights reserved.
//

#import "CBDSampleReelsController.h"
#import "BaseTableView.h"
#import "CBDSampleReelsCell.h"
#import "CBDNetManager.h"
#import "CBDRecommendModel.h"
#import "CBDSampleDetailsViewController.h"
#import "CBDRecommendDateDetailCtr.h"

@interface CBDSampleReelsController () <UITableViewDelegate,UITableViewDataSource,SampleReelsCellProtocol>

@property (weak, nonatomic) IBOutlet BaseTableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *failPic;

@property (nonatomic,strong) UIView *progressLine;

@end

@implementation CBDSampleReelsController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CBDSampleReelsCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self reloadThemeColor];
    
    [self.failPic addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [self loadData];
    }]];
    
    [self loadData];
}

- (void)reloadThemeColor {
    
    self.progressLine.backgroundColor = [[APPUtils getThemeColor] colorWithAlphaComponent:0.36];

    self.failPic.image = [[UIImage imageNamed:DEFAULT_FAIL_BG] imageByTintColor:[[APPUtils getThemeColor] colorWithAlphaComponent:0.48]];
    
    [self.tableView reloadData];
}

- (void)loadData {
    
    self.failPic.hidden = YES;
    
    self.progressLine.hidden = NO;
    [UIView animateWithDuration:1.2 delay:0.f options:UIViewAnimationOptionRepeat animations:^{
        self.progressLine.left = WIDTH;
    } completion:nil];
    
    if ([APPUtils getObjectForKey:CACHE_SAMPLEREEL]) {
        
        NSData *data = [APPUtils getObjectForKey:CACHE_SAMPLEREEL];
        if (data) {
            NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            [self loadDataWithArr:arr];
        }
    }
    
    [CBDNetManager requestWithUrl:@"http://api.breadtrip.com/v2/index/" requestType:HttpRequestTypeGet parameter:nil completation:^(id object) {
        
        NSDictionary *data = object[@"data"];
        
        
        NSArray *arr = data[@"elements"];
        
        NSData *arraydata = [NSKeyedArchiver archivedDataWithRootObject:arr];
        [APPUtils saveObject:arraydata forKey:CACHE_SAMPLEREEL];
        
        [self loadDataWithArr:arr];
    } failure:^(NSError *error) {
        self.progressLine.hidden = YES;
        
        if (self.datas.count == 0) {
            self.failPic.hidden = NO;
        }else {
            self.failPic.hidden = YES;
        }

    }];
}

- (void)loadDataWithArr:(NSArray *)arr {
    
    self.progressLine.hidden = YES;
    self.failPic.hidden = YES;
    
    if (arr.count == self.datas.count) {
        return;
    }
    
    [self.datas removeAllObjects];
    for (NSDictionary *dict in arr) {
        if ([dict[@"type"] integerValue] == 1) {continue;}
        CBDRecommendModel *model = [CBDRecommendModel modelWithDictionary:dict[@"data"][0]];
        [self.datas addObject:model];
    }
    
    [self.tableView reloadData];
}

#pragma mark - SampleReelsCellProtocol
- (void)selectCellWithModel:(CBDRecommendModel *)model {
    CBDSampleDetailsViewController *detailCtr = [CBDSampleDetailsViewController new];
    detailCtr.sampleId = model.ID;
    detailCtr.author = model.author;
    detailCtr.statutBarHeih = [UIApplication sharedApplication].statusBarFrame.size.height;
    [self.navigationController pushViewController:detailCtr animated:YES];
    
//    CBDRecommendDateDetailCtr *vc = [CBDRecommendDateDetailCtr nibCtrInitialiation];
//    vc.img_url = model.cover_image_default;
//    vc.pic_title = model.author;
//    vc.destination = model.name;
//     [self.navigationController pushViewController:vc animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CBDSampleReelsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    CBDSampleReelsCell *sampleReelsCell = (CBDSampleReelsCell *)cell;
    CBDRecommendModel *model = self.datas[indexPath.row];
    sampleReelsCell.model = model;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 270;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self selectCellWithModel:self.datas[indexPath.row]];
}
- (UIView *)progressLine {
    if (_progressLine == nil) {
        _progressLine = [[UIView alloc] initWithFrame:CGRectMake(- WIDTH * 0.64, HEIGHT * 0.42, WIDTH * 0.64, 4)];
        [self.tableView addSubview:_progressLine];
    }
    return _progressLine;
}
- (BOOL)prefersStatusBarHidden {
    return false;
}


@end
